package com.example.happytime;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshWebView;

import java.util.ArrayList;
import java.util.List;
//----------------------------
public class JokeFragment extends BaseFragment{
    PullToRefreshListView jokeLv;
    List<String> data;
    ArrayAdapter<String> adapter;
    int page=2;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_joke, container, false);
        jokeLv = view.findViewById(R.id.joke_lv);//查找控件
//数据源
        data = new ArrayList<>();
        //适配器:当布局只有一个TextView时可以使用这个适配器
        adapter = new ArrayAdapter<String>(getContext(), R.layout.item_joke, R.id.item_joke_tv, data);
        jokeLv.setAdapter(adapter);
        String url = getJokeUrl(page);
        //联网
        connectNet(url);
        setRefresh();
        return view;
    }
        Handler handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what==1){
                    jokeLv.onRefreshComplete();
                }
            }
        };

    private  void setRefresh(){
        jokeLv.setMode(PullToRefreshBase.Mode.BOTH);
        jokeLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
handler.sendEmptyMessageDelayed(1,500);

            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page++;
String jokeUrl=getJokeUrl(page);//获取下一个的网址
connectNet(jokeUrl);
            }
        });
    }
    private String getJokeUrl(int page) {
String key="d9a33dc33cff730be14f3ce955a2178a";
        //String key="680ca9398eb1a6304d1ab411b66e58a1";
       // String key="aa43f18b9e0b5b27d950d23224ae7072";
String  url = "http://v.juhe.cn/joke/content/list.php?key="+key+"&page="+page+"&pagesize=10&sort=asc&time=1418745237";
//http://v.juhe.cn/joke/content/list.php?key=您申请的KEY&page=2&pagesize=10&sort=asc&time=1418745237
return url;
    }
//联网成功之后会执行的代码
    @Override
    public void onSuccess(String result) {
        JokeBean jokeBean = new Gson().fromJson(result, JokeBean.class);
        List<JokeBean.ResultBean.DataBean> list = jokeBean.getResult().getData();
        for (int i=0;i<list.size();i++){
            String s = list.get(i).getContent();
            data.add(s);
        }
        adapter.notifyDataSetChanged();
        jokeLv.onRefreshComplete();
    }
}